package org.jeecg.modules.demo.weixin.util;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

import static cn.hutool.crypto.SecureUtil.md5;


/**
 * @Author jeecg-boot
 * @Date 2023/10/27 20:17
 * @Description
 */
public class SignUtil {

    /**
     *
     * @return
     */
    public static String getSign(Map<String,String> parms, String key){
        Set<Map.Entry<String, String>> entries = parms.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        List<String> values =new ArrayList();
        while(iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            String k = String.valueOf(entry.getKey());
            String v = String.valueOf(entry.getValue());
            if(StringUtils.isNotBlank(k) && StringUtils.isNotBlank(v)){
                values.add(k + "=" + v);
            }
        }
        String stringA= StringUtils.join(values, "&");

        String stringSignTemp=stringA+"&key=" + "31k52lzTObfuyZ4Tx4KdaF1HiXhuD9Bz"; //注：key为商户平台设置的密钥key

        String sign = md5(stringSignTemp).toUpperCase(); //注：MD5签名方式
        return sign;
    }
}
